package com.project.controller;

import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.project.domain.bo.BizBuyerContractBo;
import com.project.domain.entity.BizBuyerContract;
import com.project.service.IBuyerContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;



/**
 * 出账合同Controller
 *
 * @author ruoyi
 * @date 2025-02-25
 */
@Api(tags = "出账合同")
@RestController
@RequestMapping("/project/czhtqd")
public class BuyerContractController extends BaseController {
    @Autowired
    private IBuyerContractService bizBuyerContractService;

    /**
     * 查询出账合同列表
     */
    @ApiOperation(value = "列表")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BizBuyerContractBo bizBuyerContract) {
        PageHelper.startPage(bizBuyerContract.getPageNum(), bizBuyerContract.getPageSize());
        List<BizBuyerContract> list = bizBuyerContractService.selectBizBuyerContractList(bizBuyerContract);
        return getDataTable(list);
    }

    /**
     * 查询出账合同 左上方数量统计
     */
    @ApiOperation(value = "数量统计")
    @PostMapping("/list-statistics")
    public AjaxResult listStatistics(@RequestBody BizBuyerContractBo bizBuyerContract) {
        List<HashMap<String, Integer>> list = bizBuyerContractService.listStatistics(bizBuyerContract);
        return success(list);
    }

    /**
     * 导出出账合同列表
     */
    @ApiOperation(value = "导出")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:export')")
    @Log(title = "出账合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody BizBuyerContractBo bizBuyerContract) throws Exception {
        List<BizBuyerContract> list = bizBuyerContractService.selectBizBuyerContractListConnectedTable(bizBuyerContract);
        String excelColumn = bizBuyerContract.getExcelColumn();
        String[] split = excelColumn.split(",");
        ExcelUtil<BizBuyerContract> util = new ExcelUtil<>(BizBuyerContract.class);
        util.showColumn(split);
        util.exportExcelByOrder(response, list, "出账合同登记",split);

    }

    /**
     * 获取出账合同详细信息
     */
    @ApiOperation(value = "详情")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(bizBuyerContractService.selectBizBuyerContractByID(id));
    }

    /**
     * 新增出账合同
     */
    @ApiOperation(value = "新增")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:add')")
    @Log(title = "出账合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizBuyerContract bizBuyerContract) {
        return bizBuyerContractService.insertBizBuyerContract(bizBuyerContract);
    }

    /**
     * 修改出账合同
     */
    @ApiOperation(value = "修改")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:edit')")
    @Log(title = "出账合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizBuyerContract bizBuyerContract) {
        return bizBuyerContractService.updateBizBuyerContract(bizBuyerContract);
    }

    /**
     * 删除出账合同
     */
    @ApiOperation(value = "删除")
//    @PreAuthorize("@ss.hasPermi('project:czhtqd:remove')")
    @Log(title = "出账合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bizBuyerContractService.deleteBizBuyerContractByIds(ids));
    }

    /**
     * 获取原项目信息
     * @param originalProjectName
     * @return
     */
    @ApiOperation(value = "获取原项目信息")
    @GetMapping("/getOriginalProjectInfo")
    public AjaxResult getOriginalProjectInfo(String originalProjectName){
        List<BizBuyerContract> allProjectEntities = bizBuyerContractService.getHxZxXmByName(originalProjectName);
        return AjaxResult.success(allProjectEntities);
    }

}
